function e = classify(train_size=750, C=2, verbosity)
% Calculates (and returns) the confusion matrix and prints the
% error rate for the set contained in banana.mat
% using a normally distributed bayes classifier.
%
% By default the training sets contain 750 of the elements.
%
% 2012 Maarten Inja & Chiel Kooijman.

load banana.mat;

% split the data into training sets and test sets
A_train = A(1:train_size, :);
A_test = A(1:(1000-train_size), :);
B_train = B(1:train_size, :);
B_test = B(1:(1000-train_size), :);


if verbosity > 0
	fprintf('Training Mixture of Components for Class A... ')
	fflush(stdout);
end
[LLA, mogA] = em_mog(A_train, C, 0);
if verbosity > 0
	fprintf('done\n')

	fprintf('Training Mixture of Components for Class B... ')
	fflush(stdout);
end
[LLB, mogB] = em_mog(B_train, C, 0);
if verbosity > 0
	fprintf('done\n')
end

AA = 0;
for i = [1:C]
	AA += mogA{i}.PI * mvnpdf(A_test, mogA{i}.MU, mogA{i}.SIGMA);
end

AB = 0;
for i = [1:C]
	AB += mogB{i}.PI * mvnpdf(A_test, mogB{i}.MU, mogB{i}.SIGMA);
end

BA = 0;
for i = [1:C]
	AA += mogA{i}.PI * mvnpdf(B_test, mogA{i}.MU, mogA{i}.SIGMA);
end

BB = 0;
for i = [1:C]
	BB += mogB{i}.PI * mvnpdf(B_test, mogB{i}.MU, mogB{i}.SIGMA);
end

A_correct = sum(AA > AB);
B_correct = sum(BB > BA);

e = ((2*(1000-train_size)) - (A_correct + B_correct))/(2*(1000-train_size));
